#include "SSSortInfo.h"

#include <algorithm>

void CSortInfoManager::InitData(int nSortKey, bool bDesc /*= true*/) {
  m_bDirectReverse = true;
  m_vecSortInfo.clear();
  m_vecSortInfo.push_back(CSortInfo(nSortKey, bDesc));
}

void CSortInfoManager::AddOrUpdateEx(int nSortKey, bool bDesc /*= true*/) {
  bool bExist = true;
  CSortInfo sortInfo(nSortKey, bDesc);
  for (auto pos = m_vecSortInfo.begin(); pos != m_vecSortInfo.end(); ++pos) {
    CSortInfo &refSortInfo = *pos;
    if (refSortInfo.m_nSortKey == nSortKey) {
      refSortInfo.m_bDesc = !refSortInfo.m_bDesc;
      bExist = true;
      sortInfo = refSortInfo;
      m_vecSortInfo.erase(pos);
      break;
    }
  }

  m_vecSortInfo.push_back(sortInfo);
}

void CSortInfoManager::AddOrUpdate(bool bPressCtrlKey, int nSortKey,
                                   bool bDesc) {
  m_bDirectReverse = true;
  int nOldSortKey = -1;
  if (m_vecSortInfo.size() == 1) {
    nOldSortKey = m_vecSortInfo[0].m_nSortKey;
  }

  if (bPressCtrlKey) {
    AddOrUpdateEx(nSortKey, bDesc);
  } else {
    CSortInfo sortInfo(nSortKey, bDesc);
    bool bExist = true;
    for (auto pos = m_vecSortInfo.begin(); pos != m_vecSortInfo.end(); ++pos) {
      CSortInfo &refSortInfo = *pos;
      if (refSortInfo.m_nSortKey == nSortKey) {
        refSortInfo.m_bDesc = !refSortInfo.m_bDesc;
        bExist = true;
        sortInfo = refSortInfo;
        break;
      }
    }

    Clear();
    m_vecSortInfo.push_back(sortInfo);
  }

  // 若选中的是同一类型，则可以直接进行容器反向达到逆向排序，加快排序效率
  if (m_vecSortInfo.size() == 1 && nOldSortKey == m_vecSortInfo[0].m_nSortKey) {
    m_bDirectReverse = true;
  }
}

bool CSortInfoManager::GetSortInfo(int nSortKey, CSortInfo &sortInfo) const {
  if (nSortKey <= VBOND_NULL) {
    return false;
  }

  for (auto pos = m_vecSortInfo.begin(); pos != m_vecSortInfo.end(); ++pos) {
    const CSortInfo &refSortInfo = *pos;
    if (refSortInfo.m_nSortKey == nSortKey) {
      sortInfo = refSortInfo;
      return true;
    }
  }
  return false;
}

bool CSortInfoManager::HasSortKey(int nSortKey) const {
  if (nSortKey <= VBOND_NULL) {
    return false;
  }

  for (auto pos = m_vecSortInfo.begin(); pos != m_vecSortInfo.end(); ++pos) {
    const CSortInfo &refSortInfo = *pos;
    if (refSortInfo.m_nSortKey == nSortKey) {
      return true;
    }
  }

  return false;
}

bool CSortInfoManager::HasSortKey(const std::vector<int> &vecSortKey) const {
  for (auto pos = m_vecSortInfo.begin(); pos != m_vecSortInfo.end(); ++pos) {
    const CSortInfo &refSortInfo = *pos;
    if (std::find(vecSortKey.begin(), vecSortKey.end(),
                  refSortInfo.m_nSortKey) != vecSortKey.end()) {
      return true;
    }
  }

  return false;
}

bool CSortInfoManager::IsSatisfyDirectReverse() const {
  if (m_bDirectReverse && m_vecSortInfo.size() == 1) {
    switch (m_vecSortInfo[0].m_nSortKey) {
        // 		case VBUYVOL:
        // //价格等底层缓存常变数据，为了保证数据正确性，需要每次取最新数据进行排序
        // 		case VBUYPRICE:
        // 		case VSELLVOL:
        // 		case VSELLPRICE:
      case VMODIFY_TIME:
        // 		case VESTBCLEPRICE:
        // 		case VMATURITY_DATE:
        // 		case INDICATIVE_PRICE:
        // 		case VTKNPRICE:
        // 		case BSVTKPRICE:
        // 		case kBid_EstPrice:
        // 		case kEstPrice_Ofr:
        return true;
        break;
      default:
        break;
    }

    return true;
  }

  return true;
}
